package mz.matafe.bolao

import grails.test.*

/**
 * Unit Test Class for a Team.
 *
 * @since 0.0.1
 * @author <a href="mailto:matafe@gmail.com">Maurício T. Ferraz</a>
 */
class TeamTests extends GrailsUnitTestCase {

    def group
    def teamDetails
    def team

    protected void setUp() {
        super.setUp()
        // Set up default team
        group = new Group(name: 'A')
        teamDetails = new TeamDetails(played:0,won:0,draw:0,
            lost:0,goalsFor:0,goalsAgainst:0,points:0)
        team = new Team(name: 'Brasil', teamDetails: teamDetails)
        // Make sure we can invoke validate() on our Team domain object.
        mockDomain(Group, [group])
        mockForConstraintsTests(Team, [team])
        group.addToTeams(team)
    }

    protected void tearDown() {
        super.tearDown()
    }

    /**
     * Testing toString() method
     */
    void testToString() {
        assertEquals 'Brasil', team.toString()
    }

    /**
     * Testing the nullable constraints
     */
    void testConstraintsNullable() {
        team = new Team()
        assertFalse team.validate()
        assertEquals 2, team.errors.errorCount
        assertEquals 'The name is null', 'nullable', team.errors['name']
        assertEquals 'The teamDetails is null', 'nullable',
            team.errors['teamDetails']
        team = new Team(name: 'Africa do Sul', group: group,
            teamDetails:teamDetails)
        assertTrue team.validate()
    }

    /**
     * Testing the unique constraints
     */
    void testConstraintsUnique() {
        team = new Team(name: 'Brasil', group: group,
            teamDetails: teamDetails)
        assertFalse team.validate()
        assertEquals 'The name must be unique','unique', team.errors['name']
        team = new Team(name: 'Africa do Sul', group: group,
            teamDetails: teamDetails)
        assertTrue team.validate()
    }

    /**
     * Testing the blank constraints
     */
    void testConstraintsBlank() {
        team = new Team(name: '', group: group,
            teamDetails: teamDetails)
        assertFalse team.validate()
        assertEquals 1, team.errors.errorCount
        assertEquals 'The name is blank','blank', team.errors['name']
        team = new Team(name: 'Africa do Sul', group: group,
            teamDetails: teamDetails)
        assertTrue team.validate()
    }

    /**
     * Testing the size constraints
     */
    void testConstraintsSize() {
        team = new Team(name: 'A'*31, group: group,
                teamDetails: teamDetails)
        assertFalse team.validate()
        assertEquals 'The name has more than 30 characters','size', team.errors['name']
        team.name = 'Africa do Sul'
        assertTrue team.validate()
    }
}